package com.chenqi.community.service.Impl;

import com.chenqi.community.dao.PostDao;
import com.chenqi.community.dao.TagDao;
import com.chenqi.community.entity.Post;
import com.chenqi.community.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 * @author Ardai
 */
@Service
@Transactional(rollbackFor = {RuntimeException.class})
public class PostServiceImpl implements PostService {

    @Autowired
    private PostDao postDao;

    @Autowired
    private TagDao tagDao;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Post> findByPage(Integer page, Integer rows) {
        //处理分页索引问题
        int start = (page - 1) * rows;
        List<Post> posts = postDao.findAllPostByPage(start, rows);
        return posts;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Integer findPostCount() {
        return postDao.findPostCount();
    }

    @Override
    public Post findPostById(Integer pid) {
        return postDao.findPostById(pid);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {RuntimeException.class})
    public void addPost(Post post,String[] tags) {
        post.setCreateTime(new Date());
        post.setComments(0);
        post.setView(0);
        postDao.insertPost(post);
        //在帖子进行添加以后再进行对于标签的添加
        for (int i=0;i<tags.length;i++){
            tagDao.insertTags(post.getId(),Integer.valueOf(tags[i]));
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Integer> findAllPostId() {
        return postDao.findAllPostId();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {RuntimeException.class})
    public void editPostInfo(Post post) {
        postDao.updatePostInfo(post);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Post> findPostByTitle(Integer page,Integer rows,String title) {
        int start = (page - 1) * rows;
        return postDao.findPostByTitle(start,rows,title);
    }

    @Override
    public Integer findSearchTitleCount(String title) {
        return postDao.findSearchTitleCount(title);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Integer findPostByTypeIdCount(Integer tid) {
        return postDao.findPostByTypeIdCount(tid);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Post> findPostByTypeId(Integer page, Integer rows, Integer tid) {
        int start = (page - 1) * rows;
        return postDao.findPostByTypeId(start,rows,tid);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Post> findAllPost() {
        return postDao.findAllPost();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {RuntimeException.class})
    public void removePost(Integer pid) {
        postDao.deletePostById(pid);
    }

}
